package com.j180.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.j180.bean.Customer;
import com.j180.service.CustomerServiceImpl;
import com.j180.service.ICustomerService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ShowAllCustomerServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决乱码
         req.setCharacterEncoding("utf-8");
         resp.setCharacterEncoding("utf-8");
         resp.setContentType("text/html;charset=utf-8");

         //创建业务层
        ICustomerService iCustomerService = new CustomerServiceImpl();
        //取数据
        List<Customer> list = iCustomerService.showAllCustomersInfo();
        //获得文字输出流
          PrintWriter out = resp.getWriter();
//        //显示界面
//        out.print("<html>");
//        out.print("<body>");
//        out.print("<table border='1'>");
//        out.print("<tr><td>编号</td><td>姓名</td><td>收货地址</td></tr>");
//        for (Customer customer : list) {
//            out.print("<tr><td>"+customer.getCid()+"</td><td>"+customer.getCname()+"</td><td>"+customer.getAddress()+"</td></tr>");
//        }
//        out.print("</table>");
//        out.print("</body>");
//        out.print("</html>");

        ObjectMapper objectMapper = new ObjectMapper();

        String json = objectMapper.writeValueAsString(list);

        out.print(json);


    }
}
